from django.db import models
from django.contrib.auth.models import User

# Create your models here.

#角色信息 [id,name,account,desc]
class Roles(models.Model):
    name = models.CharField(verbose_name='名称',max_length=50,null=False,blank=False,unique=False)
    desc = models.CharField(verbose_name='描述',max_length=200,null=True,blank=True,default=None)
    

    class Meta:
        verbose_name = "角色"
        verbose_name_plural = "角色"

    def __str__(self):
        return '%s' %self.name

#顶级菜单【id,title,icon,order】
class Menu(models.Model):
    title = models.CharField(verbose_name='名称',max_length=100,null=False,blank=False,unique=True)
    icon = models.CharField(verbose_name='图标',max_length=100,null=True,blank=False,default=None)
    order = models.IntegerField(verbose_name='排序',null=True,blank=True,default=1)

    class Meta:
        managed = True
        verbose_name = '一级菜单'
        verbose_name_plural = '一级菜单'

    def __str__(self):
        return "%s" %self.title


class RolesUser(models.Model):
    user = models.ForeignKey(verbose_name='用户名',to=User,related_name='users',on_delete=models.CASCADE)
    role = models.ManyToManyField(verbose_name='角色',to=Roles,related_name='role')

    class Meta:
        verbose_name = "用户与角色"
        verbose_name_plural = verbose_name


#权限 【id,title,url,roles,menu,order】
class Permission(models.Model):
    title = models.CharField(verbose_name='子名称',max_length=100,null=False,blank=False,unique=True)
    name = models.CharField(verbose_name='路由名英文',max_length=200,null=True,blank=True,default=None)
    url = models.CharField(verbose_name='URL',max_length=200,null=True,blank=True,default=None)
    component = models.CharField(verbose_name='组件名',max_length=100)
    icon = models.CharField(verbose_name='图标',max_length=100,null=True,blank=False,default=None)
    order = models.IntegerField(verbose_name='排序',null=True,blank=True,default=1)
    roles = models.ManyToManyField(verbose_name='角色',to=Roles,related_name='roles',default=None)
    menu = models.ForeignKey(verbose_name="一级菜单", 
        to=Menu, on_delete=models.PROTECT,null=True)

    class Meta:
        verbose_name = '二级菜单'
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s" %self.title